﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtDevice
{
    public class SMSInterface : ISMSInterface
    {
        AtDevice _device;

        public SMSInterface(AtDevice device)
        {
            _device = device;
        }

        private string StatusToString(MessageStatus status)
        {
            switch (status)
            {
                case MessageStatus.All:
                    return "ALL";
                case MessageStatus.ReceivedRead:
                    return "REC READ";
                case MessageStatus.ReceivedUnread:
                    return "REC UNREAD";
                case MessageStatus.StoredSent:
                    return "STO SENT";
                case MessageStatus.StoredUnsent:
                    return "STO UNSENT";
                default:
                    throw new NotSupportedException("Should not get here");
            }
        }

        private MessageStatus StatusFromString(string str)
        {
            MessageStatus status;
            switch (str)
            {
                case "REC READ":
                    status = MessageStatus.ReceivedRead;
                    break;
                case "REC UNREAD":
                    status = MessageStatus.ReceivedUnread;
                    break;
                case "STO SENT":
                    status = MessageStatus.StoredSent;
                    break;
                case "STO UNSENT":
                    status = MessageStatus.StoredUnsent;
                    break;
                default:
                    throw new NotSupportedException("Should not get here");
            }
            return status;
        }

        public List<SMS> ListMessages(MessageStatus status)
        {
            _device.ExecuteCommand(new AtCommand("CMGF").AppendInt(1));

            List<SMS> messages = new List<SMS>();
            if (status == MessageStatus.Received)
            {
                AtResponse response = _device.ExecuteCommand(new AtCommand("CMGL").AppendString(StatusToString(MessageStatus.ReceivedRead)));
                messages.AddRange(AppendMessages(response));
                response = _device.ExecuteCommand(new AtCommand("CMGL").AppendString(StatusToString(MessageStatus.ReceivedUnread)));
                messages.AddRange(AppendMessages(response));
            }
            else
            {
                AtResponse response = _device.ExecuteCommand(new AtCommand("CMGL").AppendString(StatusToString(status)));
                messages.AddRange(AppendMessages(response));
            }
            return messages;
        }

        private List<SMS> AppendMessages(AtResponse response)
        {
            List<SMS> messages = new List<SMS>();
            foreach (var item in response.Items)
            {
                messages.Add(new SMS()
                {
                    Identifier = item.GetInt(0),
                    Status = StatusFromString(item.GetString(1)),
                    Number = item.GetString(2),
                    TimeStamp = item.GetString(4),
                    Content = item.GetPdu(),
                });
            }
            return messages;
        }

        public void SendSMS(string number, string message)
        {
            _device.ExecuteCommand(new AtCommand("CMGF").AppendInt(1));
            AtResponse response = _device.ExecuteCommand(new AtCommand("CMGS").
                                                                AppendString(number).
                                                                AppendMessage(message));
            SaveSMS(number, message, true);
        }

        public void SaveSMS(string number, string message, bool sent)
        {
            _device.ExecuteCommand(new AtCommand("CMGF").AppendInt(1));
            string sentStatus = sent ? "STO SENT" : "STO UNSENT";
            _device.ExecuteCommand(new AtCommand("CMGW").AppendString(number)
                                                        .AppendInt(145)
                                                        .AppendString(sentStatus)
                                                        .AppendMessage(message));
        }

        public void SaveSMS(string number, string message)
        {
            SaveSMS(number, message, false);
        }

        public void DeleteSMS(int identifier)
        {
            _device.ExecuteCommand(new AtCommand("CMGD").AppendInt(identifier));
        }
    }
}
